package org.jeecg.modules.fk.service;

import org.jeecg.modules.fk.mapper.auto.model.*;
import org.jeecg.modules.fk.mapper.manual.model.*;
import org.jeecg.modules.fk.vo.req.CwbgPmqkDto;
import org.jeecg.modules.fk.vo.req.RiskCwbgZcjgDto;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class RiskCbService {

    @Autowired
    private FkDaoRepository daoRepository;

    public RiskCwbgZcjg getCwbgZcjg(RiskCwbgZcjgDto req) {
        RiskCwbgZcjgKey key = new RiskCwbgZcjgKey();
        BeanUtils.copyProperties(req, key);
        return this.daoRepository.getRiskCwbgZcjgMapper().selectByPrimaryKey(key);
    }

    public RiskCwbgFzjg getCwbgFzjg(RiskCwbgFzjgKey req) {
        return this.daoRepository.getRiskCwbgFzjgMapper().selectByPrimaryKey(req);
    }

    public RiskCwbgLrjg getCwbgLrjg(RiskCwbgLrjgKey req) {
        return this.daoRepository.getRiskCwbgLrjgMapper().selectByPrimaryKey(req);
    }

    public RiskCwbgZczzl getCwbgZczzl(RiskCwbgZczzlKey req) {
        return this.daoRepository.getRiskCwbgZczzlMapper().selectByPrimaryKey(req);
    }

    public RiskCwbgDbmx getCwbgDbmx(RiskCwbgDbmxKey req) {
        return this.daoRepository.getRiskCwbgDbmxMapper().selectByPrimaryKey(req);
    }

    public RiskCwbgThydb getCwbgThydb(RiskCwbgThydbKey req) {
        return this.daoRepository.getRiskCwbgThydbMapper().selectByPrimaryKey(req);
    }

    public List<RiskCwbgPmqk> getCwbgPmqk(CwbgPmqkDto req) {
        return this.daoRepository.getRiskCwbgPmqkExtMapper().getCwbgPmqkList(req.getNsrsbh(), req.getSkssqq(), req.getSkssqz(), req.getPmywDm());
    }

    /**
     * 获取资产结构分析
     *
     * @param req 请求参数
     *
     * @return 资产结构扩展对象
     */
    public RiskCwbgZcjgExt getCwbgZcjgWithPmqk(RiskCwbgZcjgKeyExt req) {
        RiskCwbgZcjgKeyExt key = new RiskCwbgZcjgKeyExt();
        BeanUtils.copyProperties(req, key);

        List<String> pmywDmList = key.getPmywDmList();
        if (true == ObjectUtils.isEmpty(pmywDmList)) {
            pmywDmList = Arrays.asList("01", "02");
            key.setPmywDmList(pmywDmList);
        }

        return this.daoRepository.getRiskCwbgZcjgExtMapper().selectByPrimaryKey(key);
    }

    /**
     * 获取负债结构分析
     *
     * @param req 请求参数
     *
     * @return 负债结构扩展对象
     */
    public RiskCwbgFzjgExt getCwbgFzjgWithPmqk(RiskCwbgFzjgKeyExt req) {
        RiskCwbgFzjgKeyExt key = new RiskCwbgFzjgKeyExt();
        BeanUtils.copyProperties(req, key);

        List<String> pmywDmList = key.getPmywDmList();
        if (true == ObjectUtils.isEmpty(pmywDmList)) {
            pmywDmList = Arrays.asList("03", "04");
            key.setPmywDmList(pmywDmList);
        }

        return this.daoRepository.getRiskCwbgFzjgExtMapper().selectByPrimaryKey(key);
    }

    /**
     * 获取利润结构分析
     *
     * @param req 请求参数
     *
     * @return 利润结构扩展对象
     */
    public RiskCwbgLrjgExt getCwbgLrjgWithPmqk(RiskCwbgLrjgKeyExt req) {
        RiskCwbgLrjgKeyExt key = new RiskCwbgLrjgKeyExt();
        BeanUtils.copyProperties(req, key);

        List<String> pmywDmList = key.getPmywDmList();
        if (true == ObjectUtils.isEmpty(pmywDmList)) {
            pmywDmList = Arrays.asList("05", "06");
            key.setPmywDmList(pmywDmList);
        }

        return this.daoRepository.getRiskCwbgLrjgExtMapper().selectByPrimaryKey(key);
    }

    /**
     * 获取资产周转率结构分析
     *
     * @param req 请求参数
     *
     * @return 资产周转率结构扩展对象
     */
    public RiskCwbgZczzlExt getCwbgZczzlWithPmqk(RiskCwbgZczzlKeyExt req) {
        RiskCwbgZczzlKeyExt key = new RiskCwbgZczzlKeyExt();
        BeanUtils.copyProperties(req, key);

        List<String> pmywDmList = key.getPmywDmList();
        if (true == ObjectUtils.isEmpty(pmywDmList)) {
            pmywDmList = Arrays.asList("07", "08");
            key.setPmywDmList(pmywDmList);
        }

        return this.daoRepository.getRiskCwbgZczzlExtMapper().selectByPrimaryKey(key);
    }

    /**
     * 获取杜邦模型分析（计算资产负债率）
     *
     * @param req 请求参数
     *
     * @return 杜邦模型扩展对象
     */
    public RiskCwbgDbmxExt getCwbgDbmxExt(RiskCwbgDbmxKey req) {
        RiskCwbgDbmxExt riskCwbgDbmxExt = new RiskCwbgDbmxExt();

        RiskCwbgDbmx riskCwbgDbmx = this.daoRepository.getRiskCwbgDbmxMapper().selectByPrimaryKey(req);
        if (null == riskCwbgDbmx) {
            return riskCwbgDbmxExt;
        }

        BeanUtils.copyProperties(riskCwbgDbmx, riskCwbgDbmxExt);

        Double zzcBq = riskCwbgDbmxExt.getZzcBq();
        if (null == zzcBq || true == zzcBq.equals(0D)) {
            return riskCwbgDbmxExt;
        }

        Double zfzBq = riskCwbgDbmxExt.getZfzBq();
        if (null == zfzBq) {
            return riskCwbgDbmxExt;
        }

        double zcfzl = zfzBq / zzcBq;
        riskCwbgDbmxExt.setZcfzl(zcfzl);

        Double zzcSq = riskCwbgDbmxExt.getZzcSq();
        Double zfzSq = riskCwbgDbmxExt.getZfzSq();

        if (true == ObjectUtils.isEmpty(zzcSq) || true == ObjectUtils.isEmpty(zfzSq) ||
                true == zzcSq.equals(0D) || true == zfzSq.equals(0D)) {
            riskCwbgDbmxExt.setZcfzlBd(0D);

            return riskCwbgDbmxExt;
        }

        double zcfzlSq = zfzSq / zzcSq;

        if (0D == zcfzlSq) {
            riskCwbgDbmxExt.setZcfzlBd(0D);

            return riskCwbgDbmxExt;
        }

        double zcfzlBd = (zcfzl - zcfzlSq) / zcfzlSq * 100D;
        riskCwbgDbmxExt.setZcfzlBd(zcfzlBd);

        return riskCwbgDbmxExt;
    }

}
